package com.txtsqlclient.ui.si;

import java.io.IOException;

import com.txtsqlclient.ui.exception.SubInterfaceException;
import com.txtsqlclient.ui.helper.UserInputCapturer;
import com.txtsqlclient.ui.vo.Session;

/**
 * @author spadda
 * 
 */
public class GenericCommandSubInterface extends PromptBasedSubInterface {

	private String command;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.txtsqlclient.ui.InputGatherer#gatherInput(com.txtsqlclient.ui.Session)
	 */
	public void gatherInput(Session session) throws SubInterfaceException {
		try {
			this.command = UserInputCapturer.getLine("sql> ");
		} catch (IOException ie) {
			throw new SubInterfaceException(
					"Error while reading user input from console:", ie);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.txtsqlclient.ui.InputHandler#handleInput(com.txtsqlclient.ui.Session)
	 */
	public void handleInput(Session session) throws SubInterfaceException {
		session.setCommand(this.command);
		if (this.command != null && this.command.trim().length() != 0) {
			SubInterface si = getSubInterfaceFactory().getCommandSubInterface(
					this.command);
			si.process(session);
		}
	}

}